package com.example.test0519.utils;

import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Sakura
 */
public class FileUtils {
    public static boolean uploadFile(byte[] aryFileBytes,String filePath) throws FileNotFoundException {
        boolean bRet = true;
        try {
            File classPathDirectory = null;
            classPathDirectory = new File(ResourceUtils.getURL("classpath:").getPath());
            if (!classPathDirectory.exists()){
                classPathDirectory = new File("");
            }
            File uploadDirectory = new File(classPathDirectory.getAbsolutePath(),
                    filePath.substring(0,filePath.lastIndexOf("/") + 1));
                    if (uploadDirectory.exists() == Boolean.FALSE){
                        uploadDirectory.mkdirs();
                    }
            FileOutputStream out = new FileOutputStream(classPathDirectory.getAbsoluteFile()+"/"+filePath);
                    out.write(aryFileBytes);
                    out.flush();
                    out.close();
        }catch (IOException e){
            e.printStackTrace();
            bRet = false;
        }
        return bRet;
    }
}
